package graphicalInterface;

import pieces.Bishop;
import pieces.King;
import pieces.Knight;
import pieces.Pawn;
import pieces.Piece;
import pieces.Queen;
import pieces.Rook;
import core.Board;

/**
 * Class: GUIBoard
 * Responsibilities: Draw a visual representation of the chess board.
 * Collaborators: None.
 * @author Tobias
 *
 */
public class GUIBoard {

	/**
	 * Draws the board to screen, given a board.
	 * @param b
	 */
	public void drawBoard(Board b) {
		String[][] board = new String[8][8];

		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board[0].length; j++) {
				board[i][j] = "-";
			}
		}

		for(Piece p : b.getPieces()) {
			board[p.getP().getColumn()][p.getP().getRow()] = translatePieceToString(p);
		}

		printTopOrBottom();

		for (int i = 0; i < board.length; i++) {
			System.out.printf("%4s",i+1); // Prints number at the sides
			for (int j = 0; j < board.length; j++) {
				System.out.printf("%4s",board[j][i]);
			}
			System.out.printf("%4s",i+1); // Prints number at the sides
			System.out.println();
		}
		printTopOrBottom();
	}

	/**
	 * Given a piece, it returns a representation of what's in it.
	 * @param p
	 * @return
	 */
	private String translatePieceToString(Piece p) {
		String returnStr = "";
		if(p.isWhite()) returnStr += "W"; else returnStr += "B";
		if(p instanceof Bishop)    returnStr += "B";
		else if(p instanceof King) returnStr += "K";
		else if(p instanceof Knight) returnStr += "Kn";
		else if(p instanceof Pawn) returnStr += "P";
		else if(p instanceof Queen) returnStr += "Q";
		else if(p instanceof Rook) returnStr += "R";
		else return null;
		return returnStr;
	}

	/**
	 * Returns a letter from an int. Only up to seven because
	 * the chess board only has 8 columns.
	 * @param int to be converted.
	 * @return Letter-representation of int.
	 */
	private String intToLetter(int i) {
		switch(i) {
		case 0: return "a";
		case 1: return "b";
		case 2: return "c";
		case 3: return "d";
		case 4: return "e";
		case 5: return "f";
		case 6: return "g";
		case 7: return "h";
		}
		return null;
	}

	/**
	 * Prints top or bottom of board.
	 */
	private void printTopOrBottom() {
		System.out.printf("    ");
		for (int i = 0; i < 8; i++) {
			System.out.printf("%4s",intToLetter(i));
		}
		System.out.println();
	}

}
